package cn.wolfcode.luowowo.redis.Service.impl;

import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.enum1.RedisKeys;
import cn.wolfcode.luowowo.redis.Service.IStatisVoRedisService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.util.DateUtil;
import cn.wolfcode.luowowo.vo.StatisVo;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class StatisVoRedisServiceImpl implements IStatisVoRedisService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IStrategyService strategyService;


    @Override
    public StatisVo getStatisVo(String sid) {
        //创建key
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        //判断key是否存在
        StatisVo vo = null;
        if (redisTemplate.hasKey(key)) {
            //如果存在.获取vo对象
            String statisVo = redisTemplate.opsForValue().get(key);
            //将其装换成对象
            vo = JSON.parseObject(statisVo, StatisVo.class);
        } else {
            //如果不存在,自己创建一个,再初始化
            vo = new StatisVo();
            //根据sid得到攻略明细对象,再设置给vo
            Strategy strategy = strategyService.selectOne(sid);
            BeanUtils.copyProperties(strategy, vo);
            //攻略id要自己设置
            vo.setStrategyId(sid);
        }
        return vo;
    }

    @Override
    public void setStatisVo(StatisVo vo) {
        //创建key
        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        //设置进redis
        redisTemplate.opsForValue().set(key, JSON.toJSONString(vo));
    }

    @Override
    public void increateViewnum(String id) {
        //每次刷新页面阅读量都加1
        //获取vo
        StatisVo statisVo = this.getStatisVo(id);
        //设置阅读量加1
        statisVo.setViewnum(statisVo.getViewnum() + 1);
        //设置回去
        this.setStatisVo(statisVo);
    }

    @Override
    public void increateReplynum(String strategyId) {
        //每次点击发表评论评论数都加1
        //获取vo
        StatisVo statisVo = this.getStatisVo(strategyId);
        //设置评论数加1
        statisVo.setReplynum(statisVo.getReplynum() + 1);
        //设置回去
        this.setStatisVo(statisVo);
    }

    @Override
    public Boolean favornum(String sid, String uid) {
        //点击收藏时,判断该用户是否已经收藏
        //获取key
        String key = RedisKeys.STRATEGY_FAVORNUM.join(uid);
        //创建一个空集合
        List<String> sids = new ArrayList<>();
        //获取vo
        StatisVo statisVo = this.getStatisVo(sid);
        //判断是否有value
        if (redisTemplate.hasKey(key)) {
            //如果有,获取value
            String s = redisTemplate.opsForValue().get(key);
            sids = JSON.parseArray(s, String.class);
        }
        if (sids.contains(sid)) {
            //直接返回true,也就是已经收藏过
            //设置收藏数+1
            statisVo.setFavornum(statisVo.getFavornum() - 1);
            //从数组中移除sid
            sids.remove(sid);
            //设置回去
        } else {
            //如果没有,创建一个,将sids作为value
            sids.add(sid);
            //设置收藏数+1
            statisVo.setFavornum(statisVo.getFavornum() + 1);
            //设置回去
        }
        this.setStatisVo(statisVo);
        String s1 = JSON.toJSONString(sids);
        redisTemplate.opsForValue().set(key, s1);
        //返回,看数组中是不是还包含sid,包含则说明成功收藏+1,否则为false,收藏-1
        return sids.contains(sid);
    }

    @Override
    public List<String> getSids(String sid, String userId) {
        //创建key
        String key = RedisKeys.STRATEGY_FAVORNUM.join(userId);
        List<String> sids = new ArrayList<>();
        //判断redis中是否存在
        if (redisTemplate.hasKey(key)) {
            //获取value
            String s = redisTemplate.opsForValue().get(key);
            sids = JSON.parseArray(s, String.class);
        }
        return sids;
    }

    @Override
    public Boolean strategyThumbup(String sid, String uid) {
        //顶处理
        //获取key
        String key = RedisKeys.STRATEGY_THUMBUP.join(sid, uid);
        //判断是否存在key
        if (redisTemplate.hasKey(key)) {
            //有的话说明已经顶过了,今天内不能再顶了
            return false;
        }
        //没有的话说明还没顶过,要自己设置key
        //设置时间有效时间
        Date date = new Date();
        Date endDate = DateUtil.getEndDate(date);
        long time = DateUtil.getDateBetween(date, endDate);
        redisTemplate.opsForValue().set(key, "1", time, TimeUnit.SECONDS);
        //获取vo
        StatisVo statisVo = this.getStatisVo(sid);
        //设置顶加1
        statisVo.setThumbsupnum(statisVo.getThumbsupnum()+1);
        //设置回去
        this.setStatisVo(statisVo);
        return true;
    }


}
